/**
 * Created by IntelliJ IDEA.
 * User: Iliyav
 * Date: 22.03.2010
 * Time: 20:23:10
 * To change this template use File | Settings | File Templates.
 */
package com.prime.tutorial.moviestore.service.logicalview;


import javax.jws.WebMethod;
import javax.jws.WebService;

import javax.xml.ws.Endpoint;



import com.prime.tutorial.moviestore.dao.LogicalViewDAO;

import com.prime.tutorial.moviestore.domain.*;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * SOAP Web-Service endpoint
 * @author dstrelok
 * @author iryna.masyuk
 */
@WebService(name = "LogicalViewWSService")
public class LogicalViewService extends SpringBeanAutowiringSupport {
    @Autowired
    public LogicalTest logicalTest;

    /**
     * Provides the list of countries with present devices
     * @param i dummy argument
     * @return  list of countries
     */
    @WebMethod()
    public List<Country> getCountriesWithDevices(int i) {
        List<Country> ll = logicalTest.getCountriesWithDevices();
//        logicalViewDAO.getCountriesWithDevices();
        ArrayList<Country> lc=new ArrayList<Country>(ll);

        return lc;
    }

    /**
     * Provides the list of the cities in the country with countryId, where the devices are present
     * @param countryId
     * @return list of cities
     */
    @WebMethod()
    public List<City> getCitiesWithDevices(int countryId) {
        return logicalTest.getCitiesWithDevices(countryId);
    }

    /**
     * Provides core devices(i.e. core reouters) int the city with cityId
     * @param cityId id of the city
     * @return list of devices
     */
    @WebMethod()
    public ArrayList<Device> getCoreDevices(int cityId) {
        ArrayList<Device> ld=new ArrayList<Device>(logicalTest.getCoreDevices(cityId));
        return ld;
    }

    /**
     * Returns the list of the devices, connected to the device
     * @param deviceId id of the device, whose children we're looking for
     * @return list of the devices
     */
    @WebMethod()
    public ArrayList<Device> getChildDevices(int deviceId) {
        ArrayList<Device> ld=new ArrayList<Device>(logicalTest.getChildDevices(deviceId));
        return ld;
    }

    /**
     * Returns the connections(ie. ports etc) of the given device
     * @param deviceId  id of the device
     * @return list of DeviceToDevice
     */
    @WebMethod()
    public List<DeviceToDevice> getChildConnections(int deviceId){
        return logicalTest.getChildConnections(deviceId);
    }

    /**
     * Returns the ports of the given device
     * @param deviceId
     * @return list of ports
     */
    @WebMethod()
    public List<Port> getPorts(int deviceId){
        return logicalTest.getPorts(deviceId);
    }

    /**
     * Returns the deviceTypes by device id
     * @param deviceId
     * @return list of ModelType
     */
    @WebMethod()
    public List<ModelType> getDeviceTypesByDeviceId(int deviceId){
        //TODO test
        return logicalTest.getDeviceTypesByDeviceId(deviceId);
    }

    /**
     * Creates a new device with STATE_PLANNED_FOR_INSTALLATION
     * @param deviceName name of the device
     * @param typeName name of the model
     * @param street name of the street
     * @param buidling name of the building
     * @param apartment name of the apartment
     * @param description optional description of the device
     * @param countryName name of the country
     * @param cityName name of the city
     * @param x x-coordinate for logical map
     * @param y y-coordinate for logical map
     * @return Device if the device is creted successfully, null otherwise
     */
    @WebMethod()
    public Device addDevice(String deviceName,
                            String typeName,
                            String street,
                            String buidling,
                            String apartment,
                            String description,
                            String countryName,
                            String cityName,
                            double x,
                            double y){
       return logicalTest.addDevice(deviceName,
               typeName,
               street,
               buidling,
               apartment,
               description,
               countryName,
               cityName,
               x,y);
    }

    /**
     * Adds the connection between two ports
     * @param fromPortId port of the parent device
     * @param toPortId port of the child device
     * @return true if the connection is created, false otherwise
     */
    @WebMethod()
    public boolean addConnection(int fromPortId, int toPortId){
        return logicalTest.addConnections(fromPortId,toPortId);
    }

    /**
     * Removes the connection between two ports
     * @param fromPortId port of the parent device
     * @param toPortId port of the child device
     * @return true if the connection is removed, false otherwise
     */
    @WebMethod()
    public boolean deleteConnection(int fromPortId, int toPortId){
        //TODO not working need query
        //return logicalTest.deleteConnection(fromPortId,toPortId);
        return false;
    }

    /**
     * Removes the device
     * @param deviceId id of the device to be removed
     * @return true if the device is removed, false otherwise
     */
    @WebMethod()
    public boolean deleteDevice(int deviceId){
        //TODO not working
        //return logicalTest.deleteDevice(deviceId);
        return true;
    }

    /**
     * Returns the list of models by type
     * @param deviceId
     * @param typeName
     * @return
     */
    @WebMethod()
    public List<Model> getModelsByType(int deviceId, String typeName){
        //TODO test
        return logicalTest.getModelsByType(deviceId,typeName);
    }

    /**
     * Sets the new position of the device
     * @param deviceId id of the device
     * @param x        new x-coordinate
     * @param y        new x-coordinate
     * @return         true if success, false otherwise
     */
    @WebMethod()
    public boolean changeDevicePosition(int deviceId, double x, double y){
        return logicalTest.changeDevicePosition(deviceId,x,y);
    }

    /**
     * Changes the name of the device
     * @param deviceId id of the device
     * @param name     new name
     * @return         true if success, false otherwise
     */
    @WebMethod()
    public boolean changeDeviceName(int deviceId, String name){
        return logicalTest.changeDeviceName(deviceId, name);
    }

    /**
     * Just a getter
     * @return
     */
    public LogicalTest getLogicalTest() {
        return logicalTest;
    }

    /**
     * Just a setter
     * @param logicalTest
     */
    public void setLogicalTest(LogicalTest logicalTest) {
        this.logicalTest = logicalTest;
    }

}
